let v = new Vue({
    el:'#app',
    data:{
        email:'',
        username:'',
        password:'',
        password2:'',
        checkUsernameMsg:' ',
        checkEmailMsg:' ',
        checkPawMsg:' '
    },
    methods:{
        checkEmail:function (){
            let regEmail = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
            if (!regEmail.test(this.email)){
                this.checkEmailMsg="邮箱格式不正确，请重新输入";
            }else{
                axios({
                    url:`checkEmail/${this.email}`
                }).then(response=>{
                    if (response.data.flag){
                        this.checkEmailMsg="";
                    }else{
                        this.checkEmailMsg=response.data.data;
                    }
                }).catch(error=>{
                    layer.msg(error.message);
                })
            }
        },
        checkUserName:function (){
            let regName = /(^[a-zA-Z0-9_-]{4,16}$)|(^[\u2E80-\u9FFF]{2,8})/;
            if (!regName.test(this.username)){
                this.checkUsernameMsg="用户名不规范，用户名可以是2-8位中文或者4-16位英文和数字的组合";
            }else{
                axios({
                    url:`checkUserName/${this.username}`
                }).then(response=>{
                    if (response.data.flag){
                        this.checkUsernameMsg="";
                    }else{
                        this.checkUsernameMsg=response.data.data;
                    }
                }).catch(error=>{
                    layer.msg(error.message);
                })
            }
        },
        checkPassword:function (){
            if (this.password2 === '' || this.password!==this.password2){
                this.checkPawMsg="两次输入密码不一致";
            }else{
                this.checkPawMsg="";
            }
        },
        toRegister:function (){
            if(this.checkUsernameMsg!=='' && this.checkEmailMsg!=='' && this.checkPawMsg!==''){
                layer.msg("信息填写有误，请检查一下");
                return false;
            }
            axios({
                url:`doRegister`,
                method:'POST',
                data:{
                    'username':this.username,
                    'email':this.email,
                    'password':this.password
                }
            }).then(response=>{
                if (response.data.flag){
                    layer.msg('注册成功，3秒后跳转登录页');
                    this.email='';
                    this.username='';
                    this.password='';
                    this.password2='';
                    let t = this;
                    setTimeout(()=>{
                        location.href='/toLogin' },3000)
                }else{
                    layer.msg(response.data.data);
                }
            })
        }
    }
})